package com.neu.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.google.gson.Gson;
import com.neu.MainActivity;
import com.neu.R;
import com.neu.bean.SucResult;
import com.neu.util.VolleyUtil;

import java.util.HashMap;
import java.util.Map;

import androidx.appcompat.app.AppCompatActivity;

public class LoginActivity extends AppCompatActivity {

    private TextView tvLogin;
    private EditText edUserName;
    private EditText edPwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    private void initView() {
        tvLogin = findViewById(R.id.tvLogin);
        tvLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegActivity.class);
                startActivity(intent);
            }
        });
        edUserName = (EditText) findViewById(R.id.edUserName);
        edPwd = (EditText) findViewById(R.id.edPwd);

    }

    /**
     * 在登录
     *
     * @param view
     */
    public void btnOKClick(View view) {

       submit();


//        Intent intent=new Intent(LoginActivity.this,MainActivity.class);
//        //登录成功,发信息跳转到个人中心
//        intent.putExtra("login","success");
//        startActivity(intent);
    }

    private void submit() {
        // validate
        String edUserNameString = edUserName.getText().toString().trim();
        if (TextUtils.isEmpty(edUserNameString)) {
            Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
            return;
        }

        String edPwdString = edPwd.getText().toString().trim();
        if (TextUtils.isEmpty(edPwdString)) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }

        // TODO validate success, do something
        //登录逻辑
        String url = "/prod-api/api/login";
        //参数
        Map<String,String> param=new HashMap<>();
        param.put("username",edUserNameString);
        param.put("password",edPwdString);

        VolleyUtil.request(Request.Method.POST, url, param, new VolleyUtil.BackJson() {
            @Override
            public void success(String json) {
                Gson gson=new Gson();
                SucResult result=gson.fromJson(json,SucResult.class);
                if(result.getCode()==200){//成功
                    //将token存入
                    VolleyUtil.Token=result.getToken();
                    //跳转到MainActivity
                    Intent intent=new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                }else{//登录失败
                    Toast.makeText(LoginActivity.this,"用户名密码错误",Toast.LENGTH_LONG).show();
                }
            }
        });

    }
}